# coding=utf-8

import sys

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_moment import Moment

from app.oracle_conn_pool_ac1 import OrclPoolAC1
from app.oracle_conn_pool_ac2 import OrclPoolAC2
from app.oracle_conn_pool_ac3 import OrclPoolAC3
from app.oracle_conn_pool_ac4 import OrclPoolAC4
from app.oracle_conn_pool_cen import OrclPoolCEN
from app.oracle_conn_pool_param import OrclPoolPARAM
from config import config

bootstrap = Bootstrap()
moment = Moment()
oraConnAC1 = OrclPoolAC1()
oraConnAC2 = OrclPoolAC2()
oraConnAC3 = OrclPoolAC3()
oraConnAC4 = OrclPoolAC4()
oraConnCEN = OrclPoolCEN()
oraConnPARAM = OrclPoolPARAM()


# 使用程序工厂函数初始化程序实例
def create_app(config_name):
    app = Flask(__name__)  # 实例化Flask对象
    reload(sys)
    sys.setdefaultencoding('utf8')  # 设置字符集
    app.config.from_object(config[config_name])  # 导入配置
    config[config_name].init_app(app)  # 初始化Flask对象
    bootstrap.init_app(app)
    moment.init_app(app)

    # 注册蓝本 main
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint, url_prefix='/main')

    # 注册蓝本 auth
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    return app
